{% extends 'base.html' %}
{% block main_section %}
<section class="main-section blog-section" id="blog-posts">
  <span class="loading">loading blog posts ...</span>
</section>
{% endblock %}
{% block extra_inline_js %}

{% if blog_platform == 'wordpress' %}
var postOffset = 0,
    wpDomain = '{{ wp_blog_url }}';
{% else %}
var postOffset = 0;
{% endif %}

$(function() {
  setupLinks();
  fetchBlogPosts(postOffset, "{{tag_slug}}", "{{ blog_platform }}");

  {% if DISQUS_INTEGRATION_ENABLED %}
  $('body').bind('blog-post-loaded', function() {
    embedDisqus(true);
  });
  {% endif %}
});

var resultsLoaded = false,
    reachedEnd    = false, // set to true if no more blog posts left.
    scrollWait    = false,
    scrollWaitDur = 250;

$(window).scroll(function() {
  if(!reachedEnd && !resultsLoaded && !scrollWait &&
      ($(window).scrollTop() + $(window).height() > $(document).height()/1.2)) {
      resultsLoaded = true;
      postOffset += 20;
      fetchBlogPosts(postOffset, "{% if tag_slug %}{{tag_slug}}{% endif%}", '{{ blog_platform }}');
      scrollWait = true;
      // Only load posts at most every scrollWaitDur milliseconds.
      setTimeout(function() { scrollWait = false; }, scrollWaitDur);
  }
  if(resultsLoaded && ($(window).scrollTop() +
    $(window).height() < $(document).height()/1.2)) {
      resultsLoaded = false;
  }
});
{% endblock %}
